<!-- html5 --> 
<!DOCTYPE HTML>
<html>
<head>
<link rel="stylesheet" type="text/css" href="css/template.css">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>HappyMeal Management System</title>
</head>

<body>
<div id='all'>
<div id='all-wrap'>

    <!--header begin-->
	<div id='wrap-header'>
		<?php
			include 'header.php';
			//Forbid admin or blocked restaurant from adding a new meal
			if(false && $_SESSION['id'] != $_GET['id']){
				// Do not allow to add meal
				header("Refresh: 2;url=meal.php");
   				die("You do not have persmission to edit this meal. Click <a href='meal.php'>here</a> if your browser cannot redirect");
			}
		?>
	</div>
    <!--header end-->
    
    <!--wrap-body begin-->
    <div id='wrap-body'>
        <!-- wrap-search begin-->
        <div id='wrap-search'>
       
        </div>
		<!-- wrap-search end-->
        
        <!-- wrap-content begin-->
		<div id='wrap-content'>
        	<div id='nav' style="padding-left:30px;">
               <h4>Current : <a href='main.php'>Main</a> <?php include 'nav_png.php'; ?> <a href='meal.php'>Meal</a> <?php include 'nav_png.php'; ?> Meal Edit</h4>
			</div>
        	<!-- partner_add begin-->
			<div id='partner_add'>
            	
                <?php
					$id = $_GET['id'];
					$sql_partner_id = "SELECT * FROM meal WHERE id='$id'";
					$sql_partner_id_result = mysql_query($sql_partner_id);
					if(mysql_num_rows($sql_partner_id_result)==1){
						$sql_partner_id_result_row = mysql_fetch_array($sql_partner_id_result);
						
						$id 		= $sql_partner_id_result_row['id'];
						$restaurant = $sql_partner_id_result_row['restaurant'];
						$name 		= $sql_partner_id_result_row['name'];
						$description = $sql_partner_id_result_row['description'];
						$tag 		= $sql_partner_id_result_row['tag'];
						$picture 	= $sql_partner_id_result_row['picture'];
						$price		= $sql_partner_id_result_row['price'];
						$is_published = $sql_partner_id_result_row['is_published'];
						$is_blocked	= $sql_partner_id_result_row['is_blocked'];
						
					}else{
						//someone typing insame id
						header("Refresh: 2;url=meal.php");
						die("Invalid meal ID entered.");	
					}
                ?>
            	
				<form action='meal.php' method='post' id='meal_edit_form' enctype="multipart/form-data">
				<h3 style='padding-left:50px'>Please enter the information of a new meal.</h3>
                <table>
                
                <tr>
				<td width="150sp">ID:</td>
                <?php
                	echo "<td><input type='text' name='id' value='".$id."' readonly='readonly'></td>";
				?>
				</tr>
                
                <!-- Name of meal -->
				<tr>
				<td width="150sp">Name:</td><td><input type='text' name='name' value='<?php echo $name?>'></td>
				</tr>
                
                <!-- Description -->
                <tr>
				<td width="150sp">Description:</td><td><textarea rows="4" cols="50" name="description" form="meal_edit_form"><?php echo $description; ?></textarea></td>
				</tr>
                
                <!-- Tag -->
                <tr>
				<td width="150sp">Tag:</td>
                <td>
                <table width="100%">
                 <?php
					$tag;
					$tag_array = explode(',', $tag);
					
					$num_of_pair_of_tag = 5;
					for($i = 0; $i < $num_of_pair_of_tag; $i++){
						echo "<tr>";
						echo "<td><input type='text' name='tag[]' value='".$tag_array[(2*$i)]."'></td>";
						echo "<td><input type='text' name='tag[]' value='".$tag_array[(2*$i+1)]."'></td>";
						echo "</tr>";
					}
					
				?>
                </table>
                </td>
                </tr>
                
                <!-- Meal Picture -->
                <tr>
				<td width="150sp">Picture:</td><td><img src='<?php echo $picture; ?>' style="max-width:300px;"/><br><input type='file' name='picture'><br>Picture must be less than 1000kb</td>
				</tr>
                
                <!-- Price -->
                <tr>
				<td width="150sp">Price:</td><td><input type='number' name='price' value='<?php echo $price;?>'></td>
				</tr>

				<!-- Is published -->
                <tr>
				<td width="150sp">Published? :</td><td><select name='is_published' value='<?php echo $is_published;?>'>
                <option value=''>----SELECT----</option>
                <option value='0' <?php if($is_published=='0'){echo "selected='selected'";}?>>NO</option>
                <option value='1' <?php if($is_published=='1'){echo "selected='selected'";}?>>YES</option>
															</select></td>
				</tr>
                
                <!-- Is blocked? -->
                <tr>
				<td width="150sp">Blocked? :</td><td><?php
                									if($_SESSION['is_admin'] == 1){
														//For admin only
													?>
                <select name='is_blocked' value='<?php echo $is_blocked; ?>'>
                    <option value=''>----SELECT----</option>
                    <option value='0' <?php if($is_blocked=='0'){echo "selected='selected'";}?>>NO</option>
                    <option value='1' <?php if($is_blocked=='1'){echo "selected='selected'";}?>>YES</option>
				</select>
                                                   <?php
													}else{
														//For non admin
														echo "<input type='text' name='is_admin' value='".$is_blocked."' readonly='readonly'><br>";
														echo "You do not have the permission to edit this field";
													}
												   ?>
				</td>
                </tr>
                
                <!-- Submit -->
				<tr>
				<td></td><td align="left">
                	<input type='submit' name='meal_edit' value='Save' onclick="return promptConfirm()">
                	<input type='submit' name='meal_delete' value='Delete' onclick='return promptConfirmDelete()'>
					<input type='button' name='meal_cancel' value='Cancel' onclick='window.location="meal.php";' /></td>
				</tr>
				</table>
				</form>
			</div>
            <!-- meal_edit end -->
        </div>
        <!-- wrap-content end-->
        
	</div>
    <!--wrap-body end-->
    
    <!--wrap-footer begin-->
    <div id='wrap-footer'>
    	<?php
			include 'footer.php';
		?>
    </div>
    <!--wrap-footer end-->
    
</div>
<!-- all-wrap end-->
</div>
<!-- all end-->
</body>
</html>